/*
 * SubDate.java
 *
 * Copyright (C) 2005-2006 Tommi Laukkanen
 * http://www.substanceofcode.com
 *
 * Created on January 6th 2005
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

package com.substanceofcode.carmanager.model;

import java.util.Date;
import java.util.Calendar;

/**
 *
 * @author Tommi Laukkanen
 */
public class SubDate {
    
    private Date m_date;
    
    /** Creates a new instance of SubDate */
    public SubDate(Date date) {
        m_date = date;
    }
    
    /** Get date */
    public Date getDate() {
        return m_date;
    }
    
    /** Get year */
    public int getYear() {
        Calendar c = Calendar.getInstance();
        c.setTime( m_date );
        return c.get(Calendar.YEAR);
    }
    
    /** Get month */
    public int getMonth() {
        Calendar c = Calendar.getInstance();
        c.setTime( m_date );
        return c.get(Calendar.MONTH)+1;
    }
    
    /** Get day */
    public int getDay() {
        Calendar c = Calendar.getInstance();
        c.setTime( m_date );
        return c.get(Calendar.DAY_OF_MONTH);
    }
    
    /** Set year */
    public void setYear(int year) {
        Calendar c = Calendar.getInstance();
        c.setTime( m_date );
        c.set( Calendar.YEAR, year );
        m_date = c.getTime();
    }

    /** Set month */
    public void setMonth(int month) {
        Calendar c = Calendar.getInstance();
        c.setTime( m_date );
        c.set( Calendar.MONTH, month-1 );
        m_date = c.getTime();
    }
    
    /** Set day */
    public void setDay(int day) {
        Calendar c = Calendar.getInstance();
        c.setTime( m_date );
        c.set( Calendar.DAY_OF_MONTH, day );
        m_date = c.getTime();
    }    
    
    /** Is this date older than given date */
    public boolean isOlder(SubDate date) {
        if( getYear() < date.getYear() )
            return true;
        else if( getYear() > date.getYear() )
            return false;
        if( getMonth() < date.getMonth() )
            return true;
        else if(getMonth() > date.getMonth())
            return false;
        if( getDay() < date.getDay() )
            return true;
        else if( getDay() > date.getDay() )
            return false;
        return true;
    }

    /** Is this date newer than given date */
    public boolean isNewer(SubDate date) {
        if( getYear() < date.getYear() )
            return false;
        else if( getYear() > date.getYear() )
            return true;
        if( getMonth() < date.getMonth() )
            return false;
        else if(getMonth() > date.getMonth())
            return true;
        if( getDay() < date.getDay() )
            return false;
        else if( getDay() > date.getDay() )
            return true;
        return true;
    }    
    
}
